﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace GFluentCode.Tools
{
    /// <summary>
    /// 随机密钥
    /// </summary>
    public class RandomKeyHelper
    {
        /// <summary>
        /// 字符
        /// </summary>
        static char[] constant = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
        };
        /// <summary>
        /// 纯数字
        /// </summary>
        static char[] constant2 = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

        /// <summary>
        /// 获取随机数
        /// </summary>
        /// <param name="Length"></param>
        /// <returns></returns>
        public static string GenerateRandomNumber(int Length)
        {
            Stopwatch sw = new Stopwatch();
            System.Text.StringBuilder newRandom = new System.Text.StringBuilder(Length);
            sw.Start();
            for (int i = 0; i < Length; i++)
            {
                sw.Stop();
                var seed = (int)sw.ElapsedTicks;
                var rd = new Random(seed);
                newRandom.Append(constant[rd.Next(0, 61)]);
                sw.Start();
            }
            sw.Stop();
            return newRandom.ToString();
        }
        /// <summary>
        /// 纯数字随机码
        /// </summary>
        /// <param name="Length"></param>
        /// <returns></returns>
        public static string GenerateRandomNumber2(int Length)
        {
            Stopwatch sw = new Stopwatch();
            System.Text.StringBuilder newRandom = new System.Text.StringBuilder(Length);

            sw.Start();
            for (int i = 0; i < Length; i++)
            {
                sw.Stop();
                var seed = (int)sw.ElapsedTicks;
                var rd = new Random(seed);
                newRandom.Append(constant2[rd.Next(0, 9)]);
                sw.Start(); 
            }
            sw.Stop();
            return newRandom.ToString();
        }
    }
}
